//
//  MyAddressController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/10.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "MyAddressController.h"
#import "MyAddressHeadView.h"
#import "MyAddressViewCell.h"
#import "MyAddressModel.h"

#import "ChangeAddressNewsController.h"
#import "AddLocationController.h"

@interface MyAddressController ()<UITableViewDelegate,UITableViewDataSource,MyAddressViewCellDelegate>
@property (weak, nonatomic) IBOutlet UIView *AddressBackView;
@property (weak, nonatomic) IBOutlet UITableView *AddressTable;
@property (strong, nonatomic) NSMutableArray * AddressArr;
@property (strong, nonatomic) MBProgressHUD * hud;

@end

@implementation MyAddressController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self UpLoadDataOfMyAddressController];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"收货地址";
    UIButton * leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"MainBackImage_W"] forState:UIControlStateNormal];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(5,0,5,20);
    [leftBtn addTarget:self action:@selector(ClickLeftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,KScreenW,KScreenH/2);
    gl.startPoint = CGPointMake(0, 0);
    gl.endPoint = CGPointMake(1, 1);
    gl.colors = @[(__bridge id)[UIColor colorWithRed:255/255.0 green:109/255.0 blue:1/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:251/255.0 green:41/255.0 blue:67/255.0 alpha:1].CGColor];
    gl.locations = @[@(0.0),@(1.0f)];
    
    [self.AddressBackView.layer addSublayer:gl];
    
    self.AddressTable.rowHeight = 75;
    self.AddressTable.delegate = self;
    self.AddressTable.dataSource = self;
    self.AddressTable.backgroundColor = KRGBColor(243, 243, 243);
    self.AddressTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.AddressTable registerNib:[UINib nibWithNibName:@"MyAddressViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
}

- (void)UpLoadDataOfMyAddressController{
    
    [self.AddressArr removeAllObjects];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
    [dic setObject:KUserID forKey:@"address_member"];
    self.hud = [MainObjectWay CreateHUDView:self.view];
    
    [MainObjectWay PostWithMethod:@"address_list" andAction:@"Pos" andAddParam:YES parameters:dic success:^(id responseObject) {
        NSLog(@"请求的地址数据 = %@",responseObject);
        
        if ([MainObjectWay IsSucceedWithCode: responseObject[@"code"]]) {
            NSMutableArray * arr = responseObject[@"data"];
            for (int i = 0; i < arr.count; i ++) {
                NSMutableDictionary * dic = arr[i];
                MyAddressModel * model = [MyAddressModel mj_objectWithKeyValues:dic];
                [self.AddressArr addObject:model];
            }
        }
        [self.AddressTable reloadData];
        [self.hud hideAnimated:YES];
    } failure:^(NSError *error) {
        [self.hud hideAnimated:YES];
        NSLog(@"错误信息 = %@",error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.AddressArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyAddressViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[MyAddressViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.tag = indexPath.row;
    cell.delegate = self;
    [cell CreateMyAddressViewCell];
    
    MyAddressModel * model = self.AddressArr[indexPath.row];
    [cell setModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyAddressModel * model = self.AddressArr[indexPath.row];
    [self.delegate ClickOnCellOfChooseAddressWithModel:model];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)ToChangTheAddressNewsWithCellID:(NSUInteger)CellID{
    
    MyAddressModel * model = self.AddressArr[CellID];
    ChangeAddressNewsController * ChangeC = [[ChangeAddressNewsController alloc] init];
    ChangeC.ChangeModel = model;
    [self.navigationController pushViewController:ChangeC animated:YES];
    
}

- (IBAction)ClickOnBtnToAddLoaction:(id)sender {
    
    AddLocationController * AddC = [[AddLocationController alloc] init];
    [self.navigationController pushViewController:AddC animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.AddressArr.count == 0) {
        return KScreenH-TopH;
    }else{
        return 10;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    MyAddressHeadView * headV = [[NSBundle mainBundle] loadNibNamed:@"MyAddressHeadView" owner:self options:nil][0];
    if (self.AddressArr.count == 0) {
        headV.HeadImage.hidden = NO;
    }else{
        headV.HeadImage.hidden = YES;
    }
    
    return headV;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, 10)];
    return footV;
}

- (void)ClickLeftBarButtonItem{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSMutableArray *)AddressArr{
    if (!_AddressArr) {
        _AddressArr = [NSMutableArray array];
    }
    return  _AddressArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
